package com.smile.frame.api.demo.base.permission;

import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author LGC
 * @date 2021/10/13 20:57

 */
@Slf4j
@Component
public class PermissionKeyFactory {
    public static Set<Class<? extends PermissionKeyInterface>> permissionKeyClassSet;
    public static Map<String, PermissionKeyInterface> urlMaps = new HashMap<>();

    static {
        Reflections reflections = new Reflections("com.smile");
        permissionKeyClassSet = reflections.getSubTypesOf(PermissionKeyInterface.class);
        log.info("permission_key_class_size:" + permissionKeyClassSet.size());
    }

    @PostConstruct
    public void init() {
        for (Class<? extends PermissionKeyInterface> clazz : permissionKeyClassSet) {
            log.info("name:" + clazz.getSimpleName());
            PermissionKeyInterface[] enumConstants = clazz.getEnumConstants();
            for (PermissionKeyInterface obj : enumConstants) {
                urlMaps.put(obj.getUrl(), obj);
                log.info("enumName:" + obj + "  permission_key:" + obj.getPermission_key() + "  url:" + obj.getUrl() + "  is_authority:" + obj.getIs_authority());
            }
        }
    }
}
